<?php

namespace app\api\controller\homepage;


use app\admin\model\vactivities\Activity as act;
use app\admin\model\vactivities\Ticket;
use app\common\controller\Api;
use app\common\library\Iden;
use think\Db;


// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域

/**
 * 个人主页 不需要登录
 */
class Account extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    protected $catemodel = null;
    protected $tagsmodel = null;
    protected $actmodel = null;
   // private $actpmodel = null;
    protected  $ticket_model = null;
    protected  $fmodel = null;
    protected $cmodel = null;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\member\Account;
        $this->model = new \app\admin\model\Admin;
        $this->actmodel = new \app\admin\model\vactivities\Activity;
        $this->catemodel = new \app\admin\model\superdata\Activitycategory;
        $this->tagsmodel = new \app\admin\model\superdata\Activitytags;
        $this->ticket_model = new \app\admin\model\vactivities\Ticket;
        $this->api_act = new \app\api\controller\activity\Activity;
        $this->fmodel = new \app\admin\model\AdminFollow;//关注表
        $this->cmodel = new \app\admin\model\member\ActivityCollect;
    }




    /**
     * @desc 个人主页信息
     * @param $uid
     * */
    public function index(){

        $uid = $this->request->param("uid");
        if (!$uid) {
            $this->error(__('获取参数错误'),'',0);
        }
        $uid =  Iden::decrypt($uid);
       // echo $uid;die;
        $row = $this->model->get(['id' => $uid]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row = $row->toArray();

        if(!$this->auth->isLogin()){
            $admin_id = 0;
        }else{
            $uInfo = $this->auth->getUserInfo();
            if(!empty($uInfo)){//未登录
                // $this->error(__('Please login first'), null, 401);
                $admin_id = Iden::decrypt($uInfo['id']);
            }else{
                $admin_id = 0;
            }
        }
        $r =[];
        $t = Db::name("admin_basic_zoom")->where('admin_id', $uid)->find();

        $r['avatar'] = isset($t['avatar']) ? $t['avatar']:$row['avatar'];
        $r['username'] = isset($row['username']) ? $row['username']:'';
        $r['nickname'] = isset($row['nickname']) ? $row['nickname']:'';
        $r['introduction'] = isset($t['intro']) ? $t['intro'] :$row['introduction'];
        $r['mark'] = 0;//评分

        $acnt = $this->actmodel->getActCntByUid($uid);//活动数量
        if(!empty($acnt)){
            $r['act_num'] = $acnt;
        }else{
            $r['act_num'] = 0;
        }
        $r['col_num'] = $this->cmodel::CollectCnt($uid);//收藏数量
        $r['catArr'] = $this->model->getCatByUid($uid);
        $r['tagsArr'] = $this->model->getTagsByUid($uid);
        $r['uid'] = Iden::encrypt($uid);
        if(empty($admin_id) || empty($uid)){
            $r['isfollow'] = 0;
        }else{
            $v =  $this->fmodel->isfollow($admin_id,$uid);
            if(empty($v)|| $v == 'null'){
                $r['isfollow'] = 0;
            }else{

                $r['isfollow'] = 1;
            }
        }


        $this->success(__('读取成功'), $r,200);
    }




    /*
     * 个人主页活动列表
     */
    public function list(){
        $uid = $this->request->get("uid");
        $tag = $this->request->get("tag",1);//活动类型 1 即将开始 2 已结束
        if (!$uid) {
            $this->error(__('获取用户参数错误'),'',0);
        }
        $typeArr = [1,2];
        if (!in_array($tag, $typeArr)){
            $this->error(__('类型参数错误'),'',0);
        }

        $nowTime = time();
        if(!empty($tag)){
            if($tag == act::ACTIVITY_TAG_1){
                $map = " (startTime+duration) > $nowTime and status = ".act::ACTIVITY_STATUS_1 ;
            }else if($tag == act::ACTIVITY_TAG_2){
                $map = " (startTime+duration) <= $nowTime and status = ".act::ACTIVITY_STATUS_1 ;
            }
        }else{
            $map = "  status = ".act::ACTIVITY_STATUS_2 ;
        }
        $uid =  Iden::decrypt($uid);
        $per_page = $this->request->get('per_page', 1);
        $limit  = (int)$this->request->get('limit', 20);
        $offset = ($per_page - 1) * $limit;
        $w['admin_id'] = $uid;
       // $map['isend'] = $type;
        $list = $this->actmodel
            ->where($map)
            ->where($w)
            ->order('startTime ASC')
            ->limit($offset, $limit);
        $r = $list->select();
        $count = count($r);
        $list = [];

        foreach($r as $k=>$row){
            $row->visible(['id','name','thumb','startTime','duration','summary','category','tags','detail',
                'price','admin_id','nickname','avatar','link_url','introduction','price_min','price_max',
                'isticket','series_id']);
            $tlist = $this->ticket_model->getTicketList($row['id'],Ticket::TICKET_TYPE_2,$row['admin_id']);
            if($tlist){
                $price = self::max_min($tlist);
            }
            $list[$k]['id'] = Iden::encrypt($row['id']);
            $list[$k]['name'] = $row['name'];
            $list[$k]['price_min'] = isset($price) && !empty($price['min']) ? $price['min']:0;
            $list[$k]['price_max'] = isset($price) && !empty($price['max']) ? $price['max']:0;

            $list[$k]['summary'] = $row['summary'];
            $list[$k]['isticket'] = $row['isticket'];
            $list[$k]['isseries'] = isset($row['series_id']) && ($row['series_id'] > 0) ? 1:0;
            if($this->auth->isLogin()){
                //判断是否登录
                $list[$k]['iscollect'] = $this->api_act->Iscollect($row['id']);//是否已收藏
            }else{
                $list[$k]['iscollect'] = 0;//是否已收藏
            }

            $list[$k]['duration'] = s_to_hs($row['duration']);
            $list[$k]['startTime'] = date('Y-m-d H:i:s',$row['startTime']);

            if($row['thumb']){
                $thumbArr = explode(',',$row['thumb']);
                $list[$k]['thumb'] = !empty($thumbArr) ? $thumbArr:'';
            }

        }

        $result = array("total" =>$count, "rows" => $list,'tag'=>!empty($tag) ? $tag:1,'uid'=>Iden::encrypt($uid));

        $this->success(__('读取成功'), $result, 200);
    }

    /*
    * $tlist 获取的价格数组
    * 最大值与最小值
    */
    static public function max_min($tlist){
        if(empty($tlist))
            return false;
        $m = [];
        foreach($tlist as $v){
            $m[] = $v['price'];
        }

        $max = max($m);
        $min = min($m);
        unset($m);
        $data['max'] = $max;
        $data['min'] = $min;
        //$price = $min.'--'.$max;
        return $data;
    }

}
